import { Body, Controller, Delete, Get, HttpException, HttpStatus, Param, Patch, Post, Query } from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { EEnableStatus } from '@/shared/types/globalEnum'
import { CreateNoticeDto, ListPaginationNoticeDto, UpdateNoticeConfigDto, UpdateNoticeDto } from './dto/notice.dto'
import { NoticeConfigService, NoticeService } from './notice.service'

@ApiTags('后台/文章')
@Controller('admin/notice')
export class NoticeController {
  constructor(
    private readonly noticeService: NoticeService,
    private readonly noticeConfigService: NoticeConfigService,
  ) {}

  /**
   * 获取文章配置
   */
  @Get('config')
  configDetail() {
    return this.noticeConfigService.findOne()
  }

  /**
   * 更新文章配置
   */
  @Patch('config')
  async updateConfig(@Body() data: UpdateNoticeConfigDto) {
    const findData = await this.noticeConfigService.findOne()
    if (!findData) {
      throw new HttpException('文章配置不存在', HttpStatus.NOT_FOUND)
    }

    this.noticeConfigService.update(findData.noticeConfigId, data)
  }

  /**
   * 文章列表
   */
  @Get()
  list(
    @Query()
    query: ListPaginationNoticeDto,
  ) {
    return this.noticeService.findPage(query)
  }

  /**
   * 文章详情
   */
  @Get(':noticeId')
  detail(
    @Param('noticeId')
    noticeId: number,
  ) {
    return this.noticeService.findOne({
      noticeId,
    })
  }

  /**
   * 创建文章
   */
  @Post()
  async create(
    @Body()
    createData: CreateNoticeDto,
  ) {
    if (createData.noticeIsTop === EEnableStatus.ENABLED) {
      await this.noticeService.setAllDisable()
    }
    return this.noticeService.create(createData)
  }

  /**
   * 更新文章
   */
  @Patch(':noticeId')
  async update(
    @Param('noticeId')
    noticeId: number,
    @Body()
    updateData: UpdateNoticeDto,
  ) {
    const findData = await this.noticeService.findOne({
      noticeId,
    })
    if (!findData) {
      throw new HttpException('文章不存在', HttpStatus.NOT_FOUND)
    }
    if (updateData.noticeIsTop === EEnableStatus.ENABLED) {
      await this.noticeService.setAllDisable()
    }
    await this.noticeService.update(noticeId, updateData)
  }

  /**
   * 删除文章
   */
  @Delete(':noticeId')
  async remove(
    @Param('noticeId')
    noticeId: number,
  ) {
    const findData = await this.noticeService.findOne({
      noticeId,
    })
    if (!findData) {
      throw new HttpException('首页配置不存在', HttpStatus.NOT_FOUND)
    }
    await this.noticeService.remove(noticeId)
  }
}

@ApiTags('官网/文章')
@Controller('web/notice')
export class WebNoticeController {
  constructor(
    private readonly noticeService: NoticeService,
    private readonly noticeConfigService: NoticeConfigService,
  ) {}

  /**
   * 获取文章列表
   */
  @Get()
  webList(
    @Query()
    query: ListPaginationNoticeDto,
  ) {
    return this.noticeService.findPage(query)
  }

  /**
   * 获取文章配置
   */
  @Get('config')
  configDetail() {
    return this.noticeConfigService.findOne()
  }

  /**
   * 获取文章详情
   */
  @Get(':noticeId')
  webDetail(
    @Param('noticeId')
    noticeId: number,
  ) {
    return this.noticeService.findOne({
      noticeId,
    })
  }
}
